#定义一个自己的绝对值函数
def my_abs(x):
    #参数检查，只有int和float可以传入
    if not isinstance(x, (int, float)):
        raise TypeError("bad params")
    if(x>=0):
        return x
    else:
        return -x

#定义一个空函数,用pass来标识占位不做任何事情
def my_tip():
    pass

#次方函数,n=2标识设置默认2次方,默认参数必须放到后面,必选参数在前
def my_power(x,n=2):
    result = 1
    i = 1
    while i<=n:
        result = result * x
        i = i + 1
    return result

def enroll(name, gender):
    print("姓名:", name)
    print("性别:", gender)
    
#定时默认参数时如果L不设置None，由于指向了一个可变变量，多次不传参数执行会一直增加END
def add_end(L=None):
    if L is None:
        L = []
    L.append("END")
    return L

def cal(numbers):
    sum = 0
    for num in numbers:
        sum = sum + num *num
    return sum

# 测试参数错误提示
# my_abs("test")

# print(my_power(3))

# print("list", cal([1,2]))
# print("tuple", cal((1,2,3)))

# print("可变参数", cal2(1,2,3))

#可变参数，通过加*实现，接收的是一个tuple，也可以一个参数都不传返回0
def cal2(*numbers):
    sum = 0
    for num in numbers:
        sum = sum + num *num
    return sum
#可变函数直接套用list,tuple
# list=[1,2,3]
# tuple=(1,2)
# print(cal(list))
# print(cal(tuple))
# print(cal2(*list))
# print(cal2(*tuple))

#递归函数，阶乘
def fact(n):
    if n==1:
        return 1
    return n * fact(n-1)
# print(fact(10))

def my_trim(s):
    length = len(s)
    if(length<=0):
        return "空字符"
    if(s[0]==" "):
        s = s[1:length]
    length = len(s)
    if(length<=0):
        return "空字符"
    if(s[length-1]==" "):
        s = s[0:length-1]
    return s
# print(my_trim("空格 空格2 "))

def find_max_and_min(L):
    min = L[0]
    max = L[0]
    for l in L:
        if(l<min):
            min = l
        if(l>max):
            max = l
    print("最小值为:%s,最大值为:%s" % (min, max))
    
# L = list(range(1,100))
# find_max_and_min(L)

def fib(n):
    i,a,b = 0,0,1
    L = []
    L.append(b)
    while i<n:
        a,b = b,a+b
        i = i+1
        L.append(b)
    print("结果值", L)
# fib(10)

#高阶函数
def add(x,y,f):
    return f(x) + f(y)

# print(add(-5,2,abs))

#闭包-多次调用inc()，每次返回值都不同
def inc():
    x = 0
    def fn():
        nonlocal x
        x = x + 1
        return x
    return fn